package com.sali.custom_endpoint;

import jakarta.annotation.Resource;
import org.springframework.boot.actuate.endpoint.annotation.*;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 自定义 actuator 端点-->获取上下文中所有的 bean
 * @author: sali
 * @date: 2025/1/4 下午6:38.
 * @version: 1.0
 */
@Component
@Endpoint(id = "sali-webs")
public class StatusBeansEndPoint {

    @Resource
    private ApplicationContext applicationContext;

    // Get请求读取内容信息
    @ReadOperation
    public Map<String, Object> getBeans() {
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        Map<String,Object> map = new HashMap<>();
        for (String beanDefinitionName : beanDefinitionNames) {
            map.put(beanDefinitionName,applicationContext.getBean(beanDefinitionName).getClass().getName());
        }
        return map;
    }

    @ReadOperation
    public Object getBeansByName(@Selector String name) {
        Object bean = applicationContext.getBean(name);
        return bean.getClass();
    }

    //Post请求写入修改数据
    @WriteOperation
    public void writer() {

    }

    //DELETE请求写入修改数据
    @DeleteOperation
    public void delete() {

    }

}
